package de.fhaugsburg.games.boardgames.scrabble;

import java.util.List;

import de.fhaugsburg.games.boardgames.Board;
import de.fhaugsburg.games.boardgames.CircularList;
import de.fhaugsburg.games.boardgames.Player;
import de.fhaugsburg.games.boardgames.PlayerMode;
import de.fhaugsburg.games.boardgames.RandomPool;
import de.fhaugsburg.games.boardgames.exceptions.GameException;
import de.fhaugsburg.games.boardgames.scrabble.exceptions.NoEmptySquareException;

/**
 * The scrabbleengine manage putting of a letter, the calculation of the points,
 * the player order, etc.
 * 
 * @author r4smus
 * 
 */
public class ScrabbleEngine {

	public enum State {
		INITIAL, DROPPING, COMMITED;
	}

	private ScrabbleBoard board;
	private CircularList<ScrabblePlayer> playerContainer;
	private ScrabblePlayer currentPlayer;
	private PlayerMode playerMode = PlayerMode.MULTI;
	private RandomPool<LetterPiece> pool;
	private State currenState;

	public ScrabbleEngine() {
		currenState = State.INITIAL;
		board = new ScrabbleBoard();
		pool = new RandomPool<LetterPiece>();
		initPool();
		playerContainer = new CircularList<ScrabblePlayer>();

	}

	/**
	 * put a piece on an specific empty square.
	 * @param piece
	 * @param row
	 * @param column
	 * @throws GameException
	 */
	public void setPiece(LetterPiece piece, int row, int column)
			throws GameException {
		if (!(board.isEmpty(row, column)))
			throw new NoEmptySquareException(row, column);
		currentPlayer.giveAway(piece);
		board.putPiece(piece, row, column);
		

	}

	public ScrabblePlayer getCurrentPlayer() {
		return currentPlayer;
	}

	public void initCurrentPlayer() {
		this.currentPlayer = playerContainer.getAll().get(0);
	}

	public ScrabbleBoard getBoard() {
		return board;
	}

	/**
	 * sets the playerMode in order of a String.
	 * 
	 * @param mode
	 */
	public void setPlayerMode(String mode) {
		if (mode.equals("MP"))
			this.playerMode = PlayerMode.MULTI;
		if (mode.equals("SP"))
			this.playerMode = PlayerMode.SINGLE;
	}

	public PlayerMode getPlayerMode() {
		return playerMode;
	}

	private void initPool() {
		for (LetterPiece letter : LetterPiece.values()) {
			for (int number = 0; number < letter.getCount(); number++) {
				pool.put(letter);
			}
		}
	}
	public void initPlayerContainer(String[] input) throws GameException{

		for (String name : input) {
			playerContainer.add(new ScrabblePlayer(name));			
		}
		givePlayersLetters();
		
		
	}
	
	public void givePlayersLetters() throws GameException{
		int lettersLeft;
		for (ScrabblePlayer player : playerContainer.getAll()) {
		     lettersLeft = 7 - player.getMyPieces().size();
		     if(lettersLeft == 0)
		    	 return;		     
		     for (int i = 0; i < lettersLeft; i++) {
				player.receive(pool.take());
			}
		}
	}
	
	public void setState(State state) throws GameException{
		currenState = state;
			
		switch (state) {
		case INITIAL:
			
			break;
		case DROPPING:
			
			break;
		case COMMITED:
			currentPlayer = playerContainer.next();
			givePlayersLetters();
			
			break;

		default:
			break;
		}
			
	}

}
